﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wwtfly.Utility;
using HAP = HtmlAgilityPack;
using HtmlAgilityPack;

namespace jmqx
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://www.jmqx.gov.cn/";
            Encoding defaultEncoding = Encoding.UTF8;
            HttpWebClient hwClient = new HttpWebClient(new Uri(url));
            byte[] byteResponse = hwClient.DownloadData(url);
            string responseHtml = defaultEncoding.GetString(byteResponse);
            WeatherEntity we = new WeatherEntity();
            GetWeatherHtml1(we, responseHtml);


            List<WeatherEntity> listWeather = GetWeatherHtml2(responseHtml);
        }



        private static void GetWeatherHtml1(WeatherEntity item, string resultString)
        {

            HtmlDocument htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(resultString);
            HtmlNode lblybcont = htmlDoc.GetElementbyId("lblybcont");
            HtmlNode lblTqqs = htmlDoc.GetElementbyId("lblTqqs");

            //开始取数据..
            item.Ybcont = lblybcont != null ? lblybcont.InnerText : null;
            item.YbTqqs = lblTqqs != null ? lblTqqs.InnerText : null;

        }


        private static List<WeatherEntity> GetWeatherHtml2(string resultString)
        {
            HtmlDocument htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(resultString);
            const int readOnlyTrLength = 6;
            const int readOnlyTdLength = 7;
            List<WeatherEntity> listWeather = new List<WeatherEntity>();

            string[,] weatherInfoArry = new string[readOnlyTrLength, readOnlyTdLength];

            //找table标签。并且class是oatable
            HtmlNodeCollection botTables = htmlDoc.DocumentNode.SelectNodes("//div[@class='tqybcontbot']/table[@class='oatable']");
            if (botTables == null || botTables.Count <= 0)
            {
                return listWeather;
            }
            if (botTables.Count > 1)
            {
                string info = "出现错误,预期只找到一个table集合,实际找到 " + botTables.Count + "个";
            }

            HtmlNode hnTables = botTables[0];
            HtmlNodeCollection hcTrs = hnTables.SelectNodes("tr");
            if (hcTrs != null)
            {
                if (hcTrs.Count != readOnlyTrLength)
                {
                    string info = "出现错误，预期是" + readOnlyTrLength + "个tr.实际为" + hcTrs.Count + "个";
                }
                for (int itr = 0; itr < hcTrs.Count; itr++)
                {

                    HtmlNode hcTr = hcTrs[itr];
                    HtmlNodeCollection hcTds = hcTr.SelectNodes("td");
                    if (hcTds.Count != readOnlyTdLength)
                    {
                        string info = "出现错误，预期是" + readOnlyTdLength + "个td.实际为" + hcTds.Count + "个";
                    }

                    if (hcTds.Count >= readOnlyTdLength)
                    {
                        for (int itd = 0; itd < hcTds.Count; itd++)
                        {
                            if (itr == 1)
                            {
                                HtmlNode hnImg = hcTds[itd].SelectSingleNode("img");
                                weatherInfoArry[itr, itd] = hnImg != null ? hnImg.GetAttributeValue("src", "") : string.Empty;
                            }
                            else
                            {
                                weatherInfoArry[itr, itd] = hcTds[itd].InnerText;
                            }
                        }
                    }                    
                }
            }
            for (int i = 0; i < readOnlyTrLength; i++)
            {

                WeatherEntity weatherE = new WeatherEntity();
                weatherE.Date = weatherInfoArry[0, i];
                weatherE.weathImgUrl = weatherInfoArry[1, i];
                weatherE.weathInfo = weatherInfoArry[2, i];
                weatherE.weathTemperature = weatherInfoArry[3, i];
                weatherE.weathWind = weatherInfoArry[4, i];
                weatherE.weathWindLevel = weatherInfoArry[5, i];
                listWeather.Add(weatherE);
            }
            return listWeather;

        }
         
    }

    public class WeatherEntity
    {
        public string Ybcont { get; set; }
        public string YbTqqs { get; set; }
        public string Date { get; set; }
        public string weathImgUrl { get; set; }
        public string weathInfo { get; set; }
        public string weathTemperature { get; set; }
        public string weathWind { get; set; }
        public string weathWindLevel { get; set; }
    }
}
